接下來還要繼續加上最後一種音效...
Player死掉的情況有兩種,一個是當血量變零的時候,一個是當Player掉出視窗。那這個death音效要把它加在player物件→Add Component→新增一個Audio Source
把death音效拉過去,音量一樣設定成0.7,Play On Awake取消打勾 。
在程式碼的部分,由於我們要在角色的HP等於0的時候去播放它,所以
先在開頭宣告AudioSource deathSound;
在Start方法裡初始化 deathSound = GetComponent<AudioSource>();
那由於角色死亡有兩種方式,所以要在兩個地方播放出death音效,所以要在這兩處加上 deathSound.Play();
分別是ModifyHp方法當Hp < 0的判斷式裡,及OnTriggerEnter2D方法碰到DeathLine的判斷式裡。
目前整個Player.cs的程式碼長這樣
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;//要使用TextMeshPro資料型態時需引入TMPro
public class Player : MonoBehaviour
{
[SerializeField] float speed = 5f;
GameObject currentFloor;
[SerializeField] int Hp;
[SerializeField] GameObject HpBar;
[SerializeField] TextMeshProUGUI scoreText;
int score;//紀錄現在到第幾level
float scoreTime;//紀錄現在過了多久時間
AudioSource deathSound;
void Start()
{
Hp = 10;
score = 0;
scoreTime = 0;
deathSound = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.D)) {
transform.Translate(speed*Time.deltaTime,0,0);
GetComponent<SpriteRenderer>().flipX = false;
GetComponent<Animator>().SetBool("run",true);
}
else if(Input.GetKey(KeyCode.A)) {
transform.Translate(-speed*Time.deltaTime,0,0);
GetComponent<SpriteRenderer>().flipX = true;
GetComponent<Animator>().SetBool("run",true);
}
else
{
GetComponent<Animator>().SetBool("run",false);
}
UpdateScore();
}
void OnCollisionEnter2D(Collision2D other) //other是指碰撞到的東西
{
if(other.gameObject.tag == "Normal")
{
if(other.contacts[0].normal == new Vector2(0f,1f))
{
Debug.Log("撞到Normal");
currentFloor = other.gameObject;
ModifyHp(1);
other.gameObject. GetComponent<AudioSource>().Play();
}
}
else if(other.gameObject.tag == "Nails")
{
if(other.contacts[0].normal == new Vector2(0f,1f))
{
Debug.Log("撞到Nails");
currentFloor = other.gameObject;
ModifyHp(-3);
GetComponent<Animator>().SetTrigger("hurt");
other.gameObject. GetComponent<AudioSource>().Play();
}
}
else if(other.gameObject.tag == "Ceiling")
{
Debug.Log("撞到天花板");
currentFloor.GetComponent<BoxCollider2D>().enabled = false;
ModifyHp(-3);
GetComponent<Animator>().SetTrigger("hurt");
other.gameObject. GetComponent<AudioSource>().Play();
}
}
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.tag == "DeathLine")
{
Debug.Log("你輸了!");
deathSound.Play();
}
}
void ModifyHp(int num)
{
Hp += num;
if(Hp > 10)
{
Hp = 10;
}
else if(Hp < 0)
{
Hp = 0;
deathSoundPlay();
}
UpdateHpBar();
}
void UpdateHpBar()
{
for(int i=0;i<HpBar.transform.childCount;i++)
{
if(Hp>i)
{
HpBar.transform.GetChild(i).gameObject.SetActive(true);
}
else
{
HpBar.transform.GetChild(i).gameObject.SetActive(false);
}
}
}
void UpdateScore()
{
scoreTime +=Time.deltaTime;//用Time.deltaTime來計算時間,因為它是每次Update方法被呼叫到的間隔時間
if(scoreTime>2f) //只要scoreTime超過2秒,就把分數+1更新到文字上,並歸零時間
{
score++;
scoreTime = 0f;
scoreText.text = "Level" + score.ToString();
}
}
}
參考網址:https://www.youtube.com/watch?v=nPW6tKeapsM&ab_channel=GrandmaCan-%E6%88%91%E9%98%BF%E5%AC%A4%E9%83%BD%E6%9C%83
音效、圖片 : 遊戲素材
(素材由安德斯提供,感謝大大)